﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.EntityFrameworkCore;
using ZhongJia.Lock.Infrastructure.Data;
using ZhongJia.Lock.Core.Domain;
using Microsoft.EntityFrameworkCore.Metadata;

namespace ZhongJia.Lock.Core.Data
{
    public class LockModelBuilder : ICustomModelBuilder
    {
        public void Build(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Community>(c =>
            {
                c.Property(p => p.Name).HasMaxLength(100);
                c.Property(p => p.Address).HasMaxLength(200);
            });

            modelBuilder.Entity<ICCard>(c =>
            {
                c.HasMany(x => x.LockDeviceMaps).WithOne(l => l.ICCard).HasForeignKey(l => l.ICCardId).OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<LockDevice>(l =>
            {
                l.HasOne(x => x.Community).WithMany().HasForeignKey(c => c.CommunityId).OnDelete(DeleteBehavior.Restrict);
                l.HasMany(x => x.ICCardMaps).WithOne(i => i.LockDevice).HasForeignKey(i=> i.LockDeviceId).OnDelete(DeleteBehavior.Restrict);
            });
        }
    }
}
